﻿#include "accountdownitem.h"
#include "ui_accountdownitem.h"
accountDownItem::accountDownItem(QString headImg,QString email,QString username,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::accountDownItem)
{
    ui->setupUi(this);
    connect(ui->item_delete,&QPushButton::clicked,[this](){
        emit onClose();
        this->close();
    });
    drawHeadImg(headImg);
    ui->item_account->setText(email);
    ui->item_name->setText(username);
}

accountDownItem::~accountDownItem()
{
    delete ui;
}

void accountDownItem::mousePressEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton) {
        emit sendUserInfo(ui->item_account->text());
    }
    //QWidget::mousePressEvent(event);不要将事件传给父级组件处理
}

void accountDownItem::drawHeadImg(QString imgRes){
    if(QFile::exists(imgRes)){
        ui->item_headImg->setStyleSheet(QString("\
                border-radius: 25px;\
                border-image:url(%1) 0 0 0 0 stretch stretch;\
        ").arg(imgRes));
    }
    else{
        ui->item_headImg->setStyleSheet("\
            border-radius:25px;\
            border-image: url(:/background/imagesRes/QQ.png) 0 0 0 0 stretch stretch;\
        ");
    }
//    int wide=2;
//    QLabel *label=ui->item_headImg;
//    QPixmap originalPixmap(imgRes);
//    QPixmap pixmap(label->size());
//    pixmap.fill(Qt::transparent); // 使用透明背景填充新的QPixmap

//    QPainter painter(&pixmap);
//    painter.setRenderHint(QPainter::Antialiasing);//设置抗锯齿
//    //默认的组合模式通常是 QPainter::CompositionMode_SourceOver，它表示新绘制的像素会覆盖现有的像素。

//    QPainterPath path;

//    //绘制头像
//    path.addEllipse(label->rect().center(), label->width()/2-wide, label->height()/2-wide);
//    painter.setClipPath(path);
//    painter.drawPixmap(0, 0,label->width(), label->height(),originalPixmap.scaled(label->width()-wide, label->height()-2*wide, Qt::KeepAspectRatio, Qt::SmoothTransformation));
//    painter.end();
//    label->setPixmap(pixmap);
}

